﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using ConSQL = Portal.Core.Data.Common.SQLCon;

namespace Portal.Core.Data.Customer
{
    public class aUsers
    {
        public static int RegisterCustomer(string UserName,string PassWord,string FullName,string Email,DateTime BirthDay,string PhoneNumber,string Avatar, string Confirm_Code)
        {
            SqlParameter[] param = new SqlParameter[9];
            param[0] = new SqlParameter("@Username", UserName);
            param[1] = new SqlParameter("@Password", PassWord);
            param[2] = new SqlParameter("@FullName", FullName);
            param[3] = new SqlParameter("@Email", Email);
            param[4] = new SqlParameter("@BirthDay", BirthDay);
            param[5] = new SqlParameter("@PhoneNumber", PhoneNumber);
            param[6] = new SqlParameter("@Avatar", Avatar);
            param[7] = new SqlParameter("@Confirm_Code", Confirm_Code);
            param[8] = new SqlParameter("@Output", SqlDbType.Int);
            param[8].Direction = ParameterDirection.Output;
            try
            {
                SqlHelper.ExecuteNonQuery(ConSQL.ConnectSQL, CommandType.StoredProcedure, "[dbo].[sp_cus_RegisterCustomer]", param);
                return Convert.ToInt32(param[8].Value);
            }
            catch
            {
                return -1;
            }
        }
        public static int ConfirmRegisterCustomer(string Email, string Confirm_Code)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@Email", Email);
            param[1] = new SqlParameter("@Confirm_Code", Confirm_Code);
            param[2] = new SqlParameter("@Output", SqlDbType.Int);
            param[2].Direction = ParameterDirection.Output; 
            try
            {
                SqlHelper.ExecuteNonQuery(ConSQL.ConnectSQL, CommandType.StoredProcedure, "[dbo].[sp_cus_ConfirmRegisterCustomer]", param);
                return Convert.ToInt32(param[2].Value);
            }
            catch
            {
                return -1;
            }
        }
        public static int DeleteRegisterCustomer(int Id)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@Id", Id);
            try
            {
                SqlHelper.ExecuteNonQuery(ConSQL.ConnectSQL, CommandType.StoredProcedure, "[dbo].[sp_cus_DeleteRegisterCustomer]", param);
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        public static int ResetPassword(string email,string repass)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@email", email);
            param[1] = new SqlParameter("@repass", repass);
            param[2] = new SqlParameter("@output", SqlDbType.Int);
            param[2].Direction = ParameterDirection.Output;
            try
            {
                SqlHelper.ExecuteNonQuery(ConSQL.ConnectSQL, CommandType.StoredProcedure, "[dbo].[sp_cus_ResetPassword]", param);
                return int.Parse(param[2].Value.ToString());
            }
            catch
            {
                return -1;
            }
        }
        public static int CheckLoginUserWeb(string email, string password)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@username", email);
            param[1] = new SqlParameter("@password", password);
            param[2] = new SqlParameter("@output", SqlDbType.Int);
            param[2].Direction = ParameterDirection.Output;
            try
            {
                SqlHelper.ExecuteNonQuery(ConSQL.ConnectSQL, CommandType.StoredProcedure, "[dbo].[sp_cus_CheckLoginWeb]", param);
                return Convert.ToInt32(param[2].Value);
            }
            catch
            {
                return -1;
            }
        }
        public static int ChangedPassword(string username, string mkcu, string mkmoi)
        {
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@username", username);
            param[1] = new SqlParameter("@mkcu", mkcu);
            param[2] = new SqlParameter("@mkmoi",mkmoi);
            param[3] = new SqlParameter("@output", SqlDbType.Int);
            param[3].Direction = ParameterDirection.Output;
            try
            {
                SqlHelper.ExecuteNonQuery(ConSQL.ConnectSQL, CommandType.StoredProcedure, "[dbo].[sp_cus_ChangedPassWord]", param);
                return Convert.ToInt32(param[3].Value);
            }
            catch
            {
                return -1;
            }
        }
        public static int UpdateInfoCustomer(int Id,string Fullname,DateTime BirthDay,string PhoneNumber,string Avatar)
        {
            object[] param = { Id, Fullname, BirthDay, PhoneNumber, Avatar };
            try
            {
                SqlHelper.ExecuteNonQuery(ConSQL.ConnectSQL, "[dbo].[sp_cus_UpdateInfoCustomer]", param);
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        public static DataSet GetInformationCustomer(int Id)
        {
            object[] param = { Id };
            return SqlHelper.ExecuteDataset(ConSQL.ConnectSQL, "[dbo].[sp_GetInformationCustomer]", param);
        }
    }
}
